package com.homework.util;

import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

import com.homework.bean.CharsetBean;
import com.sina.sae.storage.SaeStorage;

public class SaeStorageIO {

	private SaeStorageIO(){
		
	}
	
	public static boolean Upload(File file,String savePath){
		FileInputStream fis = null;
		SaeStorage ss = new SaeStorage(connDB.getAccessKey(),connDB.getSecretKey(),connDB.getAppName());
		try {
			fis = new FileInputStream(file);
			byte[] buffer = new byte[1024];
			while ((fis.read(buffer)) > 0) {
				ss.write("homework",savePath,buffer);
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally{
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}
	
	public static InputStream getInputStreamOnStorage(String address,CharsetBean charsetBean) throws Exception {
		/*------------------------------------------------------------------------ 
		  detector是探测器，它把探测任务交给具体的探测实现类的实例完成。 
		  cpDetector内置了一些常用的探测实现类，这些探测实现类的实例可以通过add方法 
		  加进来，如ParsingDetector、 JChardetFacade、ASCIIDetector、UnicodeDetector。   
		  detector按照“谁最先返回非空的探测结果，就以该结果为准”的原则返回探测到的 
		  字符集编码。 
		--------------------------------------------------------------------------*/  
		CodepageDetectorProxy detector =  CodepageDetectorProxy.getInstance();  
		/*------------------------------------------------------------------------- 
		  ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于 
		  指示是否显示探测过程的详细信息，为false不显示。 
		---------------------------------------------------------------------------*/  
		detector.add(new ParsingDetector(false));   
		/*-------------------------------------------------------------------------- 
		  JChardetFacade封装了由Mozilla组织提供的JChardet，它可以完成大多数文件的编码 
		  测定。所以，一般有了这个探测器就可满足大多数项目的要求，如果你还不放心，可以 
		  再多加几个探测器，比如下面的ASCIIDetector、UnicodeDetector等。 
		 ---------------------------------------------------------------------------*/   
		detector.add(JChardetFacade.getInstance());  
		//ASCIIDetector用于ASCII编码测定  
		detector.add(ASCIIDetector.getInstance());  
		//UnicodeDetector用于Unicode家族编码的测定  
		detector.add(UnicodeDetector.getInstance()); 	
		
		
		SaeStorage ss = new SaeStorage(connDB.getAccessKey(),connDB.getSecretKey(),connDB.getAppName());
		byte[] temp = ss.read("homework", address);
		InputStream is = new ByteArrayInputStream(temp,0,temp.length);
		
		String charsetName = detector.detectCodepage(is, temp.length).name();
		charsetBean.setCharLength(temp.length);
		charsetBean.setCharsetName(charsetName);
		return is;
	}
	
	public static boolean UploadTxt(String txt,String savePath){
		FileOutputStream outputStream;
		try {
			outputStream = new FileOutputStream(savePath);
			Writer writer = new OutputStreamWriter(outputStream);
			writer.write(txt);
			writer.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return true;
	}
	
	public static String getText(File file){
		FileInputStream inputStream;
		try {
			inputStream = new FileInputStream(file);
			Reader reader = new InputStreamReader(inputStream);
			StringBuilder filetext = new StringBuilder();
			int tempchar;
			while ((tempchar = reader.read()) != -1) {
			   filetext.append((char) tempchar);
			}
			reader.close();
			return filetext.toString();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
		
	}
	
	public static String getTextFromInputStream(InputStream is){
		FileInputStream inputStream;
		try {
			inputStream = (FileInputStream)(is);
			Reader reader = new InputStreamReader(inputStream);
			StringBuilder filetext = new StringBuilder();
			int tempchar;
			while ((tempchar = reader.read()) != -1) {
			   filetext.append((char) tempchar);
			}
			reader.close();
			return filetext.toString();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
		
	}
	
	public static InputStream getInputStream(String address){
		SaeStorage ss = new SaeStorage(connDB.getAccessKey(),connDB.getSecretKey(),connDB.getAppName());
		byte[] temp = ss.read("homework", address);
		InputStream is = new ByteArrayInputStream(temp,0,temp.length);
		return is;
	}
}
